package com.newly.driver.mqtt.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.newly.common.base.constants.CodeConstant;
import com.newly.common.base.entity.R;
import com.newly.transfer.redis.utils.RedisUtils;

public class ReturnResult {
    /**
     * 根据请求内容填充响应的token和id
     *
     * @param message
     * @return
     */
    private static JSONObject getBaseInfo(JSONObject message) {
        JSONObject body = message;
        JSONObject result = new JSONObject();
        result.put("version", "1.0");
        result.put("token", body.getStr("token"));
        result.put("id", body.getStr("id"));
        return result;
    }

    public static JSONObject successRegister(JSONObject message, JSONObject data) {
        JSONObject body = JSONUtil.parseObj(message);
        JSONObject result = new JSONObject();
        result.put("version", "1.0");
        result.put("id", body.getStr("id"));
        result.put("result", data);
        return result;
    }

    public static JSONObject error(JSONObject message, String code) {
        JSONObject result = getBaseInfo(message);
        result.put("error", 0);
        JSONObject errorResult = new JSONObject();
        switch (code) {
            case "-32700":
                errorResult.put("code", -32700);
                errorResult.put("message", "Parse error");
                result.put("id", null);
                break;
            case "-32600":
                errorResult.put("code", -32600);
                errorResult.put("message", "Invalid Request");
                break;
            case "-32601":
                errorResult.put("code", -32601);
                errorResult.put("message", "Method not found");
                break;
            case "-32602":
                errorResult.put("code", -32602);
                errorResult.put("message", "Invalid params");
                break;
            case "-32603":
                errorResult.put("code", -32603);
                errorResult.put("message", "Internal error");
                break;
            case "-32000":
                errorResult.put("code", -32000);
                errorResult.put("message", "Authentication failed");
                break;
        }
        return result;
    }

    public static R getResult(String responseTopic, RedisUtils redisUtils) {
        //间隔0.1秒轮询10次等待接收mqtt回调
        for (int i = 0; i < 10; i++) {
            String responseStr = redisUtils.get(responseTopic, String.class);
            JSONObject response = JSONUtil.parseObj(responseStr);
            if (response != null && response.get("result") != null) {
                redisUtils.del(responseTopic);
                return R.ok(response.get("result"));
            }
            if (response != null && response.get("error") != null) {
                redisUtils.del(responseTopic);
//                return R.success(response.get("error"));
                return R.fail(CodeConstant.FAILED, "处理失败");
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return R.fail("timeover");
    }
}
